﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class SocioRepository:MasterRepository,ISocioRepository
    {
        public List<SOCIO> GetAllFromSocio()
        {
            var query = from s in context.SOCIOs.Include("TIPO_SOCIO")
                        select s;
            return query.ToList();
        }

        public List<SOCIO> GetAllFromSocioByDescripcion(string descripcion)
        {
            var query = from s in context.SOCIOs.Include("TIPO_SOCIO")
                        select s;
            if (descripcion != null && descripcion != string.Empty && descripcion != "")
            {
                query = from s in context.SOCIOs
                        where s.Ruc.Contains(descripcion) ||
                                  s.RazonSocial.ToUpper().Contains(descripcion)
                        select s;
            }
            return query.ToList();
        }

        public SOCIO GetFromSocioById(int id)
        {
            var query = from s in context.SOCIOs
                        where s.IdSocio.Equals(id)
                        select s;
            return query.SingleOrDefault();
        }

        

        public void AddSocio(SOCIO socio)
        {
            socio.TIPO_SOCIO = null;
            context.SOCIOs.Add(socio);
            context.SaveChanges();
        }

        public void UpdateSocio(SOCIO socio)
        {
            context.Entry(socio).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveSocio(int id)
        {
            var existe = context.SOCIOs.Find(id);

            if (existe != null)
            {
                context.SOCIOs.Remove(existe);
                context.SaveChanges();
            }
        }


        public bool ExisteDuplicidad(string ruc, string razonSocial, int id)
        {
            var query = from s in context.SOCIOs
                        where s.Ruc.CompareTo(ruc) == 0 &&
                                  s.RazonSocial.ToUpper().CompareTo(razonSocial.ToUpper()) == 0
                                  && s.IdSocio !=id
                        select s;
            return query.Any();
        }

        public bool GetFromSocioByTipoSocioById(int idSocio)
        {
            var query = from s in context.SOCIOs.Include("TIPO_SOCIO")
                        where s.IdSocio.Equals(idSocio)
                        select s;
            return query.Any();
        }
    }
}
